import {ElementRef, EventEmitter, Output, ViewChild} from '@angular/core';
import {BaseComponent} from './BaseComponent';
import {IList} from './IList';

export abstract class BaseList extends BaseComponent implements IList {
  @Output() onAdd = new EventEmitter<{}>();
  @Output() onEdit = new EventEmitter<{ entity: any }>();
  @Output() onRemove = new EventEmitter<{ entity: any }>();
  @Output() onCancel = new EventEmitter<{ entity: any }>();

  @ViewChild('nzTable') nzTable: ElementRef;

  tempEditObject = {};

  abstract getFilter();

  abstract getGrid();

  onSearch(): void {
    this.getGrid().setParams(this.getFilter().getParams());
    this.getGrid().flushData();
  }

  _handelAdd(data) {
    if (this.onAdd) {
      this.onAdd.emit();
    }
  }

  _handelEdit(data) {
    // this.currentStep = 'add';
    this.tempEditObject[data.id] = {...data};
    if (this.onEdit) {
      this.onEdit.emit(this.tempEditObject[data.id]);
    }
  }

  _handelRemove(data) {
    Object.assign(data, this.tempEditObject[data.id]);
    if (this.onRemove) {
      this.onRemove.emit(data);
    }
  }

  _handelCancel(data) {
    this.tempEditObject[data.id] = {};
    if (this.onCancel) {
      this.onCancel.emit(data);
    }
  }


}
